#include "Robot.h"
#include "Strategy.h"
#include "RobotGrapLord.h"
#include "RobotPlayHand.h"

Robot::Robot(QObject *parent)
    : Player{parent}
{
    m_type = Player::Robot;
}

void Robot::prepareCallLord()
{
    RobotGrapLord* subThread = new RobotGrapLord(this);
    connect(subThread,&RobotGrapLord::finished,this,[=](){
        subThread->deleteLater();
    });
    subThread->start();

}

void Robot::preparePlayHand()
{
    RobotPlayHand* subThread = new RobotPlayHand(this);
    connect(subThread,&RobotGrapLord::finished,this,[=](){
        subThread->deleteLater();
    });
    subThread->start();
}

void Robot::thinkCallLord()
{
    /*
     * 基于手中的牌计算权重
     * 大小王：6
     * 顺子/炸弹：5
     * 三张：4
     * 2牌：3
     * 对牌：1
     */
    int weight = 0;
    Strategy st(this,m_cards);
    //计算大小王
    weight += st.getRangeCards(Card::Card_SJ, Card::Card_BJ).cardCount() * 6;
    //计算顺子和炸弹
    QVector<Cards> optSeq = st.pickOptimalSeqSingles();
    weight += optSeq.size() * 5;
    //计算炸弹
    QVector<Cards> bombs = st.findCardsByCount(4);
    weight += bombs.size() * 5;
    //计算2牌
    weight += m_cards.pointCount(Card::Card_2) * 3;
    //计算三张
    Cards tmp = m_cards;
    tmp.remove(optSeq);
    tmp.remove(bombs);
    Cards card2 = st.getRangeCards(Card::Card_2, Card::Card_2);
    tmp.remove(card2);
    QVector<Cards> triple = Strategy(this, tmp).findCardsByCount(3);
    weight += triple.size() * 4;
    //计算对牌
    tmp.remove(triple);
    weight += Strategy(this, tmp).findCardsByCount(2).size() * 1;

    //判断下注的分数
    if(weight >= 22){//3分
        grabLordBet(3);

    }else if(weight < 22 && weight >= 18){//2分
        grabLordBet(2);

    }else if(weight < 18 && weight >= 10){//1分
        grabLordBet(1);

    }else{//不叫
        grabLordBet(0);

    }

}

void Robot::thinkPlayHand()
{
    Strategy st(this, m_cards);
    Cards cs = st.makeStrategy();
    playHand(cs);
}
